package org.example.shop;
import java.util.HashMap;
import java.util.Map;
public class Car {

        Map<String, CarGoods> items = new HashMap<>();

        public void addItem(Goods goods, int quantity) {
            if (items.containsKey(goods.getGoodsNum())) {
                CarGoods item = items.get(goods.getGoodsNum());
                item.setQuantity(item.getQuantity() + quantity);
            } else {
                items.put(goods.getGoodsNum(), new CarGoods(goods, quantity));
            }
        }

        public void removeItem(String productId) {
            items.remove(productId);
        }

        public void updateItemQuantity(String productId, int newQuantity) {
            if (items.containsKey(productId)) {
                CarGoods g = items.get(productId);
                if (newQuantity <= 0) {
                    items.remove(productId);
                } else {
                    g.setQuantity(newQuantity);
                }       }
        }

        public double checkout(String payMethod) {
            double total = items.values().stream().mapToDouble(CarGoods::getTotalPrice).sum();
            System.out.println("使用" + payMethod + "支付" + total + "元");
            items.clear();
            return total;
        }
    }






